//
//  Product_Main_ViewController.m
//  Pacific_Computer
//
//  Created by dlios on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Product_Main_ViewController.h"
#import "Product_Main_TableViewCell.h"
#import "UIColor+AddColor.h"
#import "Product_Brand_ViewController.h"
#import "Infor_Search_ViewController.h"

@interface Product_Main_ViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UITableView *tableView;

@property (nonatomic,retain) NSMutableArray *arr;
@property (nonatomic,retain) NSMutableArray *array;
@property (nonatomic,retain) NSMutableArray *urlArr;


@end

@implementation Product_Main_ViewController

- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [_array release];
    [_urlArr release];
    [_dig release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        [self handleData];
        
        self.array = [NSMutableArray arrayWithObjects:@"手机",@"相机",@"笔记本",@"超级本",@"平板电脑",@"摄像机", nil];
        self.urlArr = [NSMutableArray arrayWithObjects:@"20937", @"20928", @"20807", @"95585", @"79849", @"20912", nil];
        
    }
    return self;
}

- (void)handleData
{
    NSString *path = @"http://mrobot.pconline.com.cn/v3/product/brandNameTopv36";
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:path];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSError *error = nil;
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        self.dig = [[Digital alloc] initWithDictionary:result];
        
        self.arr = [NSMutableArray array];
        
        NSArray *arr1 = self.dig.Mobile;
        NSArray *arr2 = self.dig.Camera;
        NSArray *arr3 = self.dig.NoteBook;
        NSArray *arr4 = self.dig.UltraBook;
        NSArray *arr5 = self.dig.TabletPc;
        NSArray *arr6 = self.dig.Vidicon;
        
        
        
        [self.arr addObject:arr1];
        [self.arr addObject:arr2];
        [self.arr addObject:arr3];
        [self.arr addObject:arr4];
        [self.arr addObject:arr5];
        [self.arr addObject:arr6];
        
        
        NSLog(@"%@",[self.arr objectAtIndex:0]);
        
        
        [_dig release];
        
        [self.tableView reloadData];
        
    }];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor  = [UIColor hongse];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.rowHeight = self.tableView.bounds.size.height * 13.94 / 100;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    [_tableView release];
    

    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(buttonClicked:)];
    
    
    
}


- (void)buttonClicked:(UIButton *)button
{
    Infor_Search_ViewController *search = [[Infor_Search_ViewController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"nstu";
    Product_Main_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[Product_Main_TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    
    cell.titleLabel.text = [self.array objectAtIndex:indexPath.row];
    
    NSMutableArray *arr = [self.arr objectAtIndex:indexPath.row];
    
    NSString *stb1 = [arr objectAtIndex:0];
    NSString *stb2 = [arr objectAtIndex:1];
    NSString *stb3 = [arr objectAtIndex:2];
    
    NSString *stb = [NSString stringWithFormat:@"%@、%@、%@",stb1,stb2,stb3];
    
    cell.brandLabel.text = stb;
    
    NSString *imageStr = [NSString stringWithFormat:@"%ld.png",(long)indexPath.row];
    
    UIImage *image = [UIImage imageNamed:imageStr];
    
    cell.myImageView.image = image;
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Product_Brand_ViewController *brand = [[Product_Brand_ViewController alloc] init];
    brand.navigationItem.title = [self.array objectAtIndex:indexPath.row];
    brand.urlNum = [self.urlArr objectAtIndex:indexPath.row];
    [brand hanldeData];
    //NSLog(@"爱卡==%@",brand.urlNum);
    brand.num = indexPath.row;
    [self.navigationController pushViewController:brand animated:YES];
    
    [brand release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
